#include <stdio.h>
#include <fcntl.h>	
#include <sys/stat.h> 	
#include <semaphore.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
/*
    /tmp/cctiQuQj.o：在函数‘main’中：
    2_semctl2.c:(.text+0x1f)：对‘sem_open’未定义的引用
    2_semctl2.c:(.text+0x66)：对‘sem_post’未定义的引用
    2_semctl2.c:(.text+0x97)：对‘sem_wait’未定义的引用
    2_semctl2.c:(.text+0xce)：对‘sem_close’未定义的引用
    2_semctl2.c:(.text+0xda)：对‘sem_unlink’未定义的引用
    collect2: error: ld returned 1 exit status

*/
int main()
{
    //1.创建一个有名信号量
    sem_t* psem = sem_open("/mytest.sem", O_CREAT, 0777);
    if (psem == SEM_FAILED)
    {
        perror("sem_open error");
        return -1;
    }

    pid_t pid = fork();
    if (pid == 0)
    {
        printf("child process!\n");
        int ret = sem_post(psem);
        if (ret == -1)
        {
            perror("chil sem_post error");
            exit(-1);
        }
    }
    else if (pid > 0)
    {
        //获取信号量
        int ret = sem_wait(psem);
        if (ret == -1)
        {
            perror("parent sem_wait error");
            exit(-1);
        }
        printf("parent process!\n");
    }


    sem_close(psem);//关闭有名信号量
    sem_unlink("/mytest.sem");//删除有名信号量
    return 0;
}